package luj.tool.jnproxyan.proxy.network.socks.cmd.connect;

import io.netty.buffer.ByteBuf;
import luj.bean.api.BeanContext;
import luj.bean.api.bean.Bean;
import luj.cluster.api.actor.Tellable;
import luj.net.api.server.FrameDataReceiver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class C2pReceiverV2 implements FrameDataReceiver {

  C2pReceiverV2(Tellable socksRef, Long reqId, BeanContext lujbean) {
    _socksRef = socksRef;
    _reqId = reqId;
    _lujbean = lujbean;
  }

  /**
   * @see luj.tool.jnproxyan.proxy.network.socks.actor.root.message.request.forward.OnClient2SForward#onHandle
   */
  @Override
  public Result receive(Context ctx) throws Exception {
    ByteBuf frameBuf = ctx.getLastFrame();
    byte[] data = new byte[frameBuf.readableBytes()];
    frameBuf.readBytes(data);

    LOG.debug("浏览器发->：#{}，大小：{}", _reqId, data.length);

//    Bean<Client2SForwardMsg> msg = _lujbean.createBean(Client2SForwardMsg.class);
//    msg.setField(m -> m::reqId, _reqId);
//    msg.setField(m -> m::data, data);
//    _socksRef.tell(msg.getValueInstance());

    return ctx.then().nextReceiver(this);
  }

  private static final Logger LOG = LoggerFactory.getLogger(C2pReceiverV2.class);

  /**
   * @see luj.tool.jnproxyan.proxy.network.socks.actor.root.Socks5Actor
   */
  private final Tellable _socksRef;

  private final Long _reqId;
  private final BeanContext _lujbean;
}
